﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MyPSDALayer;
using Newtonsoft.Json;
using MyPS.Areas.Manage.Models;
using System.Data;
using System.Data.SqlClient;

namespace MyPS.Areas.Manage.Controllers
{
    public class AjaxController : Controller
    {
        //
        // GET: /Manage/Ajax/
        MyPSEntities db = new MyPSEntities();
        public string SetArticleIndex(int id, string isindex)
        {
            var article = db.Articles.Single(o => o.ArticleID == id);
            if (isindex == "N")
            {
                article.ISIndex = "Y";
            }
            else
            {
                article.ISIndex = "N";
            }
            db.SaveChanges();
            return "Y";
        }
        [Authorize]
        public string GetMenuData()
        {
            var mode = db.Database.SqlQuery<MenuModel>(@"select distinct b.Mode,b.ResourceName,b.Url from ResoucesAcl a with(nolock) 
inner join Resources b with(nolock) on a.ResourceID=b.ResourceID 
inner join Roles c with(nolock) on a.RoleID=c.RoleID
inner join UserRoles d with(nolock) on c.RoleID=d.RoleID
where d.UserID=@UserID and a.IS_View='Y'", new SqlParameter("@UserID", SqlDbType.NVarChar, 50) { Value = HttpContext.User.Identity.Name });
            return JsonConvert.SerializeObject(mode);
        }
        [Authorize]
        public string AddRoleResource()
        {
            try
            {
                string RoleID = Request["RoleID"];
                int ResourceID = Convert.ToInt32(Request["ResourceID"]);
                var mode = db.ResoucesAcls.Find(RoleID, ResourceID);
                if (mode == null)
                {
                    ResoucesAcl acl = new ResoucesAcl();
                    acl.ResourceID = Convert.ToInt32(Request["ResourceID"]);
                    acl.ResourceName = "";
                    acl.RoleID = Request["RoleID"];
                    acl.ModifyDate = DateTime.Now;
                    acl.ModifyBy = HttpContext.User.Identity.Name;
                    acl.IS_View = Request["IS_View"];
                    acl.IS_Edit = Request["IS_Edit"];
                    acl.IS_Del = Request["IS_Del"];
                    acl.IS_Add = Request["IS_Add"];
                    acl.CreateDate = DateTime.Now;
                    acl.CreateBy = HttpContext.User.Identity.Name;
                    db.ResoucesAcls.Add(acl);
                }
                else
                {
                    mode.ResourceID = Convert.ToInt32(Request["ResourceID"]);
                    mode.ResourceName = "";
                    mode.RoleID = Request["RoleID"];
                    mode.ModifyDate = DateTime.Now;
                    mode.ModifyBy = HttpContext.User.Identity.Name;
                    mode.IS_View = Request["IS_View"];
                    mode.IS_Edit = Request["IS_Edit"];
                    mode.IS_Del = Request["IS_Del"];
                    mode.IS_Add = Request["IS_Add"];
                }
                db.SaveChanges();
                return "修改成功";
            }
            catch (Exception e)
            {
                return e.Message;
            }

        }
        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}
